package com.gcxy.controller;

import com.gcxy.core.PageVO;
import com.gcxy.core.R;
import com.gcxy.dto.user.UserPageDTO;
import com.gcxy.dto.user.PasswordDTO;
import com.gcxy.dto.user.UserInfoDTO;

import com.gcxy.service.UserService;
import com.gcxy.vo.user.UserPageVO;
import com.gcxy.vo.user.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户模块控制器
 *
 * @author pep7chiao
 * @date 2024/7/14
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public R<?> addUser(@RequestBody @Validated UserInfoDTO userInfoDTO) {
        try {
            userService.addUser(userInfoDTO);
            return R.Success();
        } catch (Exception e) {
            return R.Failed(e.getMessage());
        }
    }

    @ApiOperation("更新用户")
    @PostMapping("/updateUser")
    public R<?> updateUser(@RequestBody @Validated UserInfoDTO userInfoDTO) {
        return userService.updateUser(userInfoDTO);
    }

    @ApiOperation("根据条件分页获取所有用户")
    @PostMapping("/getUserPageByCondition")
    public R<PageVO<UserPageVO>> getUserPageByCondition(@RequestBody @Validated UserPageDTO userPageDTO) {
        return R.Success(userService.getUserPageByCondition(userPageDTO));
    }

    @ApiOperation("更新用户密码")
    @PostMapping("/updatePassword")
    public R<?> updatePassword(@RequestBody @Validated PasswordDTO passwordDTO) {
        return userService.updatePassword(passwordDTO);
    }

    @ApiOperation("批量重置用户密码")
    @PostMapping("/resetPassword")
    public R<?> resetPassword(@RequestBody @Validated List<Integer> userIds) {
        return userService.resetPassword(userIds);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUserById/{id}")
    public R<?> deleteUserById(@PathVariable Integer id) {
        return userService.deleteUserById(id);
    }

    @ApiOperation("登录时获取用户信息")
    @GetMapping("/getUserInfo")
    public R<UserVO> getUserInfo() {
        return R.Success(userService.getUserInfo());
    }


}
